//
//  MLVisitorView.swift
//  MLWeiBo
//
//  Created by wenxing on 2017/1/5.
//  Copyright © 2017年 Se7venX. All rights reserved.
//

import UIKit
import SnapKit


/*
 1.必须要遵守对象协议 NSObjectProtocol
 2.可选的话，要@objc，要让OC机制可见
 
 */
@objc protocol MLVisitorViewDelegate:NSObjectProtocol {
    
    /// 可选执行方法
    @objc optional func clickLoginButton(vistorView:MLVisitorView)
    
    /// 必须执行方法
    @objc optional func clickregisterButton(vistorView:MLVisitorView)
}


class MLVisitorView: UIView {

     //声明属性  主要需要使用weak关键字来修饰
    weak var delegate:MLVisitorViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor(white: 237.0/255, alpha: 1.0)
     
        setupUI()
    }

    //MARK:- 设置UI
    private func setupUI() {
        
        //add
        
        addSubview(circleView)
        addSubview(coverView)
        addSubview(largeHomeView)
        addSubview(tipLab)
        addSubview(loginButton)
        addSubview(registerButton)
        
        //layout
        circleView.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(self.snp.centerX)
            make.centerY.equalTo(self.snp.centerY).offset(-80)
        }
        largeHomeView.snp.makeConstraints { (make) in
            
            make.center.equalTo(circleView.snp.center)
        }
        
        coverView.snp.makeConstraints { (make) in
            
            make.center.equalTo(circleView.snp.center)
        }
        
        tipLab.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(circleView.snp.centerX)
            make.top.equalTo(circleView.snp.bottom).offset(30)
        }
        
        loginButton.snp.makeConstraints { (make) in
            
            make.left.equalTo(circleView)
            make.top.equalTo(tipLab.snp.bottom).offset(30)
            make.width.equalTo(80)
        }
        
        registerButton.snp.makeConstraints { (make) in
            
            make.right.equalTo(circleView)
            make.top.equalTo(tipLab.snp.bottom).offset(30)
            make.width.equalTo(80)
        }
    }
    
    //MARK:- 公共方法 -
    func updateInfo(title:String,imageName:String,isHome:Bool = false) {
        
        tipLab.text = title
        circleView.image = UIImage(named: imageName)
        
        if isHome {
            
            startAnimation()
            
        }else{
        
            largeHomeView.isHidden = true
            coverView.isHidden = true
        }
    }
    
    /// 动画
    private func startAnimation() {
    
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = M_PI * 2;
        
        anim.duration = 20;
        
        anim.repeatCount = MAXFLOAT
        
        anim.isRemovedOnCompletion = false
        
        circleView.layer.add(anim, forKey: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK:- Actions -
    @objc private func clickLoginButton() {
    
        delegate?.clickLoginButton!(vistorView: self)
    }

    @objc private func clickRegisterButton() {
        
        delegate?.clickregisterButton!(vistorView: self)
        
    }
    
    //MARK:- Lazy 
    lazy var circleView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    
    lazy var largeHomeView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    
    lazy var coverView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    
    lazy var tipLab: UILabel = {
        
        let l = UILabel(title: "第一次看见你，就知道会有一个很大的惊喜,就知道会有一个很大的惊喜", titleColor: UIColor.darkGray, fontSize: 14)
        l.numberOfLines = 0
        l.textAlignment = .center
        l.preferredMaxLayoutWidth = 200
        
        return l;
    }()
    

    
    lazy var loginButton: UIButton = {
        let btn = UIButton(title: "登录",backgroundImageName:"common_button_white")
        
        btn.addTarget(self, action: #selector(clickLoginButton), for: .touchUpInside)
        
        return btn
    }()
    
    
    lazy var registerButton: UIButton = {
        
        let btn = UIButton(title: "注册",titleColor:UIColor.orange,backgroundImageName:"common_button_white")
    
        
        btn.addTarget(self, action: #selector(clickRegisterButton), for: .touchUpInside)
        
        return btn
    }()
    
    
    
}




























